package com.tchintech.pay.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tchintech.pay.R;
import com.tchintech.pay.model.entity.BankList;
import com.tchintech.pay.model.entity.PaytypeResule;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/11 0011.
 * 二维码  页面支付方式展示
 */

public class RecycleCodeAdapter extends RecyclerView.Adapter<RecycleCodeAdapter.MyViewHolde> {

    private Context context;
    private List<PaytypeResule> list = new ArrayList<PaytypeResule>();
    private int oldPosition = 0;


    public RecycleCodeAdapter(Context context, List<PaytypeResule> list) {
        this.context = context;
        this.list = list;
    }

    /**
     * recycleView设置值
     * @param list
     */
    public void setData(List<PaytypeResule> list){
        if(list==null){
            list=new ArrayList<PaytypeResule>();
            this.list=list;
        }else{
            this.list=list;
        }
        notifyDataSetChanged();

    }


    //设置item的布局
    @Override
    public RecycleCodeAdapter.MyViewHolde onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.recycle_adapter, parent, false);
        MyViewHolde viewHolder = new MyViewHolde(view);
        return viewHolder;
    }


    //对item的数据绑定
    @Override
    public void onBindViewHolder(final RecycleCodeAdapter.MyViewHolde holder, final int position) {
        final PaytypeResule paytype = list.get(position);
        if(paytype.getPayType().equals("WX")){
            holder.tv_paytype.setText("微信支付");
        }else if(paytype.getPayType().equals("WEB")){
            holder.tv_paytype.setText("网关支付");
        }else if(paytype.getPayType().equals("ALI")){
            holder.tv_paytype.setText("支付宝支付");
        }else if(paytype.getPayType().equals("QUICK")){
            holder.tv_paytype.setText("快捷支付");
        }
        holder.tv_arrivtype.setText(paytype.getArrivalType());
        if (position == oldPosition) {
            //选中的状态
            holder.tv_paytype.setTextColor(ContextCompat.getColor(context,R.color.white));
            holder.tv_arrivtype.setTextColor(ContextCompat.getColor(context,R.color.white));
            ((RelativeLayout)holder.tv_arrivtype.getParent()).setBackgroundColor(ContextCompat.getColor(context,R.color.login_blue));
        } else {
            //没有选中的状态
            holder.tv_paytype.setTextColor(ContextCompat.getColor(context,R.color.gary_bg));
            holder.tv_arrivtype.setTextColor(ContextCompat.getColor(context,R.color.gary_bg));
            ((RelativeLayout)holder.tv_arrivtype.getParent()).setBackgroundColor(ContextCompat.getColor(context,R.color.white));
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mOnItemClickLitener.onItemClick( paytype, position);
            }
        });

    }







    /**
     * 设置点击高亮
     * @param position
     */
    public void changeColor(int position) {
        oldPosition = position;
        notifyDataSetChanged();

    }


    //数据源的长度
    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolde extends RecyclerView.ViewHolder {
        TextView tv_paytype;
        TextView tv_arrivtype;

        public MyViewHolde(View itemView) {
            super(itemView);
            tv_paytype = (TextView) itemView.findViewById(R.id.tv_paytype);
            tv_arrivtype = (TextView) itemView.findViewById(R.id.tv_arrivtype);

        }
    }

    public interface OnItemClickLitener {
        void onItemClick( PaytypeResule paytypeResule, int position);

    }

    private RecycleCodeAdapter.OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(RecycleCodeAdapter.OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
}

